<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" :model="userInfo" label-width="220px" :rules="rules">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <UploadImg v-model="userInfo.staffPhoto" />

          </el-form-item>
        </el-col>
      </el-row>

      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSubmit">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserAvatar } from '@/api/user'
import { updateEmployee } from '@/api/employee'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''

      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadUerInfoDetail()
  },
  methods: {
    async loadUerInfoDetail() {
      const res = await getUserAvatar(this.$route.query.id)
      console.log(res)
      this.userInfo = res.data
    },
    async doChange() {
      try {
        const res = await updateEmployee(this.userInfo)
        if (res.code === 10000 && res.success === true) {
          this.$message.success(res.message)
          this.loadUerInfoDetail()
          this.$router.back()
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.userForm.validate(valid => {
        if (!valid) return
        this.doChange()
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
